package rabbitMqLearn.spring_boot.confg;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SpringConfig {
    public static final String WORK_QUEUE = "spring_work_queue";


    //广播模式
    public static final String PUBLISH_FANOUT_EXCHANGE = "spring_publish_fanout_exchange";
    public static final String PUBLISH_FANOUT_QUEUE1 = "spring_publish_fanout_queue1";
    public static final String PUBLISH_FANOUT_QUEUE2 = "spring_publish_fanout_queue2";

    //路由模式
    public static final String PUBLISH_ROUTING_EXCHANGE = "spring_publish_routing_exchange";
    public static final String PUBLISH_ROUTING_QUEUE1 = "spring_publish_routing_queue1";
    public static final String PUBLISH_ROUTING_QUEUE2 = "spring_publish_routing_queue2";

    public static final String PUBLISH_ROUTING_KEY_1 = "spring_publish_routing_key_1";
    public static final String PUBLISH_ROUTING_KEY_2 = "spring_publish_routing_key_2";

    //通配符模式
    public static final String TOPIC_EXCHANGE = "topic_exchange";
    public static final String TOPIC_QUEUE1 = "topic_queue1";
    public static final String TOPIC_QUEUE2 = "topic_queue2";



    //工作队列模式
    //声明队列
    @Bean("workQueue")
    public Queue workQueue() {
        return QueueBuilder.durable(WORK_QUEUE).autoDelete().build();
    }

    //通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(TOPIC_QUEUE1).autoDelete().build();
    }

    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(TOPIC_QUEUE2).autoDelete().build();
    }

    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).build();
    }

    @Bean
    public Binding topicBinding(@Qualifier("topicExchange") TopicExchange exchange,@Qualifier("topicQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("#.error");
    }

    @Bean
    public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange exchange,@Qualifier("topicQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("#.info");
    }

    //路由模式
    @Bean("routingQueue1")
    public Queue routingQueue1() {
        return QueueBuilder.durable(PUBLISH_ROUTING_QUEUE1).autoDelete().build();
    }

    @Bean("routingQueue2")
    public Queue routingQueue2() {
        return QueueBuilder.durable(PUBLISH_ROUTING_QUEUE2).build();
    }

    @Bean("routingExchange")
    public DirectExchange routingExchange() {
        return ExchangeBuilder.directExchange(PUBLISH_ROUTING_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding routingBinding1(@Qualifier("routingExchange") DirectExchange routingExchange, @Qualifier("routingQueue1") Queue routingQueue) {
        return BindingBuilder.bind(routingQueue).to(routingExchange).with(PUBLISH_ROUTING_KEY_1);
    }
    @Bean
    public Binding routingBinding2(@Qualifier("routingExchange") DirectExchange routingExchange, @Qualifier("routingQueue2") Queue routingQueue) {
        return BindingBuilder.bind(routingQueue).to(routingExchange).with(PUBLISH_ROUTING_KEY_2);
    }

    //广播模式
    // 定义队列
    @Bean("fanoutQueue1")
    public Queue fanoutQueue() {
        return QueueBuilder.durable(PUBLISH_FANOUT_QUEUE1).autoDelete().build();
    }

    @Bean("fanoutQueue2")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(PUBLISH_FANOUT_QUEUE2).build();
    }

    //定义交换机
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(PUBLISH_FANOUT_EXCHANGE).durable(true).autoDelete().build();
    }

    @Bean
    public Binding fanoutBinding(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue1") Queue fanoutQueue) {
        return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange exchange, @Qualifier("fanoutQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange);
    }

}
